﻿//
// Created by QU on 24-10-5.
//

#include "SignalHandler.h"

SignalHandler::SignalHandler(): m_stop_requested(false)
{
    // system signal registered
    std::signal(SIGINT, &SignalHandler::handleSignal);
    std::signal(SIGTERM, &SignalHandler::handleSignal);
}

void SignalHandler::waitForSignal()
{
    std::unique_lock lock(m_quit_mutex);
    m_quit_cv.wait(lock, [this] { return m_stop_requested.load(); });
}

void SignalHandler::handleSignal(const int sig)
{
    if (sig == SIGINT || sig == SIGTERM)
    {
        getInstance().m_stop_requested.store(true);
        getInstance().m_quit_cv.notify_all();
    }
}

SignalHandler& SignalHandler::getInstance()
{
    static SignalHandler handler;
    return handler;
}
